package com.ykzhujiang.lovespirit;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.ykzhujiang.lovespirit.analyzer.Analyzer;
import com.ykzhujiang.lovespirit.analyzer.TimeAnalyzer;
import com.ykzhujiang.lovespirit.labor.FudanLabor;
import com.ykzhujiang.lovespirit.labor.Labor;
import com.ykzhujiang.lovespirit.labor.NanjingLabor;
import com.ykzhujiang.lovespirit.labor.NankaiLabor;
import com.ykzhujiang.lovespirit.labor.SJTULabor;
import com.ykzhujiang.lovespirit.labor.TsinghuaLabor;
import com.ykzhujiang.lovespirit.parser.FudanParser;
import com.ykzhujiang.lovespirit.parser.NanjingParser;
import com.ykzhujiang.lovespirit.parser.NankaiParser;
import com.ykzhujiang.lovespirit.parser.Parser;
import com.ykzhujiang.lovespirit.parser.SJTUParser;
import com.ykzhujiang.lovespirit.parser.TsinghuaParser;
import com.ykzhujiang.lovespirit.util.ParserProcessor;
import com.ykzhujiang.lovespirit.util.Utility;

@SuppressWarnings("serial")
public class SJTUServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setCharacterEncoding("UTF8");
		resp.setContentType("text/plain");

		Labor l = new SJTULabor();
		Parser p = new SJTUParser();
		Start(l, p, new TimeAnalyzer(7 * 3600000L * 12), resp.getWriter());
	}

	public static void Start(Labor l, Parser p, Analyzer a, PrintWriter pw) {
		if (l == null || p == null || a == null)
			return;
		l.Fetch(1);
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("threads");
		for (int i = 0; i < l.files.size(); i++) {
			ParserProcessor pp = new ParserProcessor(p, root, (String) l.files
					.get(i));
			pp.AddAnalyzer(a);
			pp.SetWriter(pw);
			pp.run();
		}
	}

	private static final Logger logger = Logger.getLogger(SJTUServlet.class
			.getName());
}
